// components/inputbox/inputbox.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        unit: {
            type: String,
            value: "份"
        },
        weight: {
            type: String,
            value: "×1"
        },
        index: {
            type: Number,
        },
        decreaseColor: {
            type: String,
            value: "gray"
        }
    },

    /**
     * 组件的初始数据
     */
    data: {},

    /**
     * 组件的方法列表
     */
    methods: {
        //获取输入的重量
        inputWeight(e) {
            this.setData({
                weight: e.detail.value
            })
        },

        // 设置减号按钮的颜色
        setDecreaseColor(weight, min) {
            if (weight == min) {
                this.setData({
                    decreaseColor: "gray"
                })
            }
        },
        // 更新缓存数据
        updateStorage(index, key, value) {
            if (index) {
                let storage = wx.getStorageSync(key);
                storage[index].weight = value;
                wx.setStorageSync(key, storage);
            }
        },

        //按“➖”按钮减少重量
        decrease(e) {
            let unit = this.data.unit;
            let weight = this.data.weight;
            if (unit == "份") {
                weight = parseInt(weight.slice(1));
                if (weight >= 2) {
                    weight--;
                    this.setData({
                        weight: "×" + weight
                    });
                    this.setDecreaseColor(weight, 1);
                }
            } else if (unit == "Kg") {
                weight = parseFloat(weight);
                if (weight >= 1) {
                    weight -= 0.5;
                    this.setData({
                        weight: weight
                    });
                    this.setDecreaseColor(weight, 0.5);
                }
            } else if (unit == "g") {
                weight = parseInt(weight);
                if (weight >= 200) {
                    weight -= 100;
                    this.setData({
                        weight: weight
                    });
                    this.setDecreaseColor(weight, 100);
                }
            } else if (unit == "两") {
                weight = parseInt(weight);
                if (weight >= 2) {
                    weight -= 1;
                    this.setData({
                        weight: weight
                    });
                    this.setDecreaseColor(weight, 1);
                }
            } else {
                weight = parseInt(weight);
                if (weight >= 1) {
                    weight -= 1;
                    this.setData({
                        weight: weight
                    });
                    this.setDecreaseColor(weight, 0);
                }
            }

            // 更新数据
            this.updateStorage(this.data.index, "buychoosed", this.data.weight);
            this.triggerEvent("updateData", this.data.weight);
            this.triggerEvent("updateRandomCHoose", [this.data.unit, this.data.weight]);
        },

        //按“➕”按钮增重量
        increase(e) {
            let unit = this.data.unit;
            let weight = this.data.weight;
            if (unit == "份") {
                weight = parseInt(weight.slice(1));
                weight++;
                this.setData({
                    weight: "×" + weight
                });
            } else if (unit == "Kg") {
                weight = parseFloat(weight) + 0.5;
                this.setData({
                    weight: weight
                });
            } else if (unit == "g") {
                weight = parseInt(weight) + 100;
                this.setData({
                    weight: weight
                });
            } else {
                weight = parseInt(weight) + 1;
                this.setData({
                    weight: weight
                });
            } 

            if (weight) {
                this.setData({
                    decreaseColor: "black"
                })
            }

            // 更新数据
            this.updateStorage(this.data.index, "buyChoosed", this.data.weight);
            this.triggerEvent("updateData", this.data.weight);
            this.triggerEvent("updateRandomCHoose", [this.data.unit, this.data.weight]);
        },
    },

    lifetimes: {
        // 根据weight是否为最小值初始化组件中的减号颜色
        attached: function () {
            let unit = this.data.unit,
                weight = this.data.weight;
            if ((unit == "份" && weight != "×1") || (unit == "Kg" && weight > 0.5) || (unit == "g" && weight > 100) || (unit == "两" && weight > 1)) {
                this.setData({
                    decreaseColor: "black"
                })
            }
        }
    }
})